# -*- encoding: utf-8 -*-
"""
@File  :   api.py
@Time  :   2021-03-09 21:30
"""
from os.path import join
from time import sleep
import sys
import json
from flask import Flask, request, jsonify
from flask.globals import g
from flask.signals import _signals
from flask import make_response
from flask_cors import CORS, cross_origin
from dbcontrol import dbconn

app = Flask(__name__)
app.config['JSON_AS_ASCII'] = False
CORS(app, supports_credentials=True, resources=r'/*')


def recursiveMenu(pid=""):
    rows = db.Query(f"select * from menu_item where pid='{pid}'")   
    mlis = []
    for row in rows:
        mid = row["menuId"]
        rows = db.Query(f"select * from menu_item where pid='{mid}'")
        if rows:
            row["children"] = recursiveMenu(mid)
        mlis.append(row)
    return mlis    

@app.route('/api/user/login', methods=["POST", "GET"])
@cross_origin()#通过装饰路线具体CORS 
def login():
    headers = {
        'Content-Type': 'application/json',    
        'Access-Control-Allow-Methods': "POST, GET, OPTIONS, DELETE"
    }
    return make_response((jsonify({'error_code': 0}), 202, headers))

@app.route('/api/user/getMenu', methods=["POST", "GET"])
def getMenu():     
    data = recursiveMenu("") 
    responseData = {
        "code": "0",
        "msg": "success",
        "count": 0,
        "pageIndex": 0,
        "pageCount": 0,
        "data": data
    }
    return jsonify(responseData)

@app.route("/")
@app.route("/index")
def index():
    return "模拟接口数据 1.0"

if __name__ == '__main__':
    db = dbconn('database.db')          
    app.run(host='0.0.0.0', port=80, debug=True)